#!/bin/bash
export BUILD_PATH=/tmp/build
#local machine installation
#export BUILD_PATH=/home/lq/work/tongmingzhiyun/api-gateway/istio-agent/docker/njet_lua_src

mkdir --verbose -p "$BUILD_PATH"
cd "$BUILD_PATH"

apt-get update
apt-get install gcc make -y
apt-get install libpcre3-dev libssl-dev zlib1g-dev perl -y \
    && apt-get clean \
    && rm -rf  /var/log/*log /var/lib/apt/lists/* /var/log/apt/* /var/lib/dpkg/*-old /var/cache/debconf/*-old

# Install luajit
#export LUAJIT_LIB=/usr/local/lib
#export LUA_LIB_DIR="$LUAJIT_LIB/lua"
#export LUAJIT_INC=/usr/local/include/luajit-2.1
export LUAJIT_INC='/etc/njet/luajit/include/luajit-2.1'
export LUAJIT_LIB='/etc/njet/luajit/lib'

cd "$BUILD_PATH/njet_main/luajit"
make CCDEBUG=-g
make install

#ln -s /usr/local/bin/luajit /usr/local/bin/lua
#ln -s "$LUAJIT_INC" /usr/local/include/lua
ln -sf /etc/njet/luajit/lib/libluajit-5.1.so.2.1.0 /usr/lib/libluajit-5.1.so
ln -sf /etc/njet/luajit/lib/libluajit-5.1.so.2.1.0 /usr/lib/libluajit-5.1.so.2

# build njet
cd "$BUILD_PATH/njet_main"

WITH_FLAGS="--with-http_ssl_module \
  --with-stream \
  --with-stream_ssl_module \
  --with-http_stub_status_module \
  --with-http_v2_module \
  --with-openssl=auto/lib/tassl "

WITH_MODULES=" \
  --add-module=$BUILD_PATH/njet_main/src/ext/lua/kit \
  --add-module=$BUILD_PATH/njet_main/src/ext/lua/http \
  --add-module=$BUILD_PATH/njet_main/modules/njet-stream-proto-module \
  --add-module=$BUILD_PATH/njet_main/modules/njet-stream-upstream-dynamic-servers-module  \
  --add-module=$BUILD_PATH/njet_main/modules/njet-http-upstream-dynamic-servers-module \
  --add-module=$BUILD_PATH/njet_main/modules/njet-http-match-module  \
  --add-module=$BUILD_PATH/njet_main/modules/njet-http-health-check-module \
  --add-module=$BUILD_PATH/njet_main/modules/njet-stream-health-check-module \
  --add-module=$BUILD_PATH/njet_main/modules/njet-http-sticky-module "

./configure \
  --prefix=/usr/local/njet \
  --lock-path=/usr/local/njet/njet.lock \
  --pid-path=/usr/local/njet/njet.pid \
  ${WITH_FLAGS} \
  --without-mail_pop3_module \
  --without-mail_smtp_module \
  --without-mail_imap_module \
  --without-http_uwsgi_module \
  --without-http_scgi_module \
  --user=1337 \
  --group=1337 \
  ${WITH_MODULES}

make
make install

cd "$BUILD_PATH/lua_module/resty-core"
make install

cd "$BUILD_PATH/lua_module/resty-balancer"
make all
make install

export LUA_INCLUDE_DIR=/usr/local/include/luajit-2.1
ln -s $LUA_INCLUDE_DIR /usr/include/lua5.1

cd "$BUILD_PATH/lua_module/resty-cjson"
make all
make install

cd "$BUILD_PATH/lua_module/resty-cookie"
make all
make install

cd "$BUILD_PATH/lua_module/resty-dns"
make install

cd "$BUILD_PATH/lua_module/resty-lock"
make install

# required for OCSP verification
cd "$BUILD_PATH/lua_module/resty-http"
make install

cd "$BUILD_PATH/lua_module/resty-ipmatcher"
INST_LUADIR=/usr/local/lib/lua make install

cd "$BUILD_PATH/lua_module/resty-global-throttle"
make install


rm -r $BUILD_PATH/*
apt-get autoremove gcc make perl -y



